/* 
 * File:   InstantComputeParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 02:43
 */

#include "InstantComputeParameterCommand.h"
#include "../../Context.h"
#include "../../parameters/Parameters.h"
#include " ../../../../memento/BooleanParameterMemento.h"

namespace calculator {

    InstantComputeParametersCommand::InstantComputeParametersCommand(const std::string name) : ParametersCommand(name) {
    }

    InstantComputeParametersCommand::InstantComputeParametersCommand(const InstantComputeParametersCommand& orig) : ParametersCommand(orig._name) {
    }

    InstantComputeParametersCommand::~InstantComputeParametersCommand() {
    }

    InstantComputeParametersCommand* InstantComputeParametersCommand::clone() const {
        return new InstantComputeParametersCommand(_name);
    }

    void InstantComputeParametersCommand::apply(Parameters* parameters) const {
        parameters->setInstantCompute(!(parameters->getInstantCompute()));
    }

    const Memento* InstantComputeParametersCommand::createMemento() const throw (CommandException) {
        return new BooleanParameterMemento(clone(), Context::getInstance()->getParameters()->getInstantCompute());
    }

    void InstantComputeParametersCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const BooleanParameterMemento* booleanParameterMemento = dynamic_cast<const BooleanParameterMemento*> (memento);
        if (booleanParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : BooleanParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setInstantCompute(booleanParameterMemento->_boolean);
    }
}